﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.Common.OA.AdminWorkingYearsRecord;
using BCService.CommonService.OA.AdminWorkingYearsRecord;
using BlackCoffeeRESTful.Configs;

namespace BlackCoffeeRESTful.Controllers.Management.OA
{

    /// <summary>
    /// 员工工作经历服务接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin-HR")]
    [Route("api/v1/sim/[controller]")]
    public class AdminWorkingYearsRecordController : ManagementBaseController
    {
        private readonly IAdminWorkingYearsRecordService adminWorkingYearsRecordService;

        public AdminWorkingYearsRecordController(IAdminWorkingYearsRecordService adminWorkingYearsRecordService)
        {
            this.adminWorkingYearsRecordService = adminWorkingYearsRecordService;
        }

        #region 创建一条员工工作经历信息
        /// <summary>
        /// 创建一条员工工作经历信息
        /// </summary>      
        /// <param name="adminWorkingYearsRecordRequestDto">员工工作经历创建请求模型</param>
        /// <response code="200">新创建的员工工作经历信息</response>
        /// <returns></returns>
        [HttpPost]
        [Log("创建一条员工工作经历信息")]
        [ProducesResponseType(typeof(AdminWorkingYearsRecordDto), 200)]
        public IActionResult Post([FromBody]AdminWorkingYearsRecordRequestDto  adminWorkingYearsRecordRequestDto)
        {        
            var adminWorkingYearsRecord = this.adminWorkingYearsRecordService.Create(adminWorkingYearsRecordRequestDto);

            return Ok(adminWorkingYearsRecord);
        }
        #endregion

        #region 修改一条员工工作经历信息

        /// <summary>
        /// 修改员工工作经历信息
        /// </summary>
        /// <param name="adminWorkingYearsRecordId">员工工作经历Id</param>
        /// <param name="adminWorkingYearsRecordSimPutDto">员工工作经历修改请求模型</param>        
        /// <returns></returns>
        /// <response code="200">修改后的员工工作经历信息。</response>
        [HttpPut("{admin_working_years_record_id}")]
        [Log("修改员工工作经历信息")]
        [ProducesResponseType(typeof(AdminWorkingYearsRecordDto), 200)]
        public IActionResult Put([FromRoute(Name = "admin_working_years_record_id")] long adminWorkingYearsRecordId, [FromBody] AdminWorkingYearsRecordSimPutDto adminWorkingYearsRecordSimPutDto)
        {
            return this.Ok(this.adminWorkingYearsRecordService.Update(adminWorkingYearsRecordId, adminWorkingYearsRecordSimPutDto));
        }

        #endregion

        #region 删除一条员工工作经历信息
        /// <summary>
        /// 删除一条员工工作经历信息
        /// </summary>
        /// <param name="adminWorkingYearsRecordId">员工工作经历Id</param>
        /// <returns></returns>
        /// <response code="204">刪除一条员工工作经历信息成功。</response>
        [HttpDelete("{admin_working_years_record_id}")]
        [Log("删除一条员工工作经历信息")]
        public IActionResult Delete([FromRoute(Name = "admin_working_years_record_id")]long adminWorkingYearsRecordId)
        {
            this.adminWorkingYearsRecordService.Delete(adminWorkingYearsRecordId);
            return NoContent();
        }
        #endregion

        #region 获取一条员工工作经历信息
        /// <summary>
        /// 获取一条员工工作经历信息
        /// </summary>
        /// <param name="adminWorkingYearsRecordId">员工工作经历ID</param>
        /// <returns></returns>
        [HttpGet("{admin_working_years_record_id}")]
        [ProducesResponseType(typeof(AdminWorkingYearsRecordDto), 200)]

        public IActionResult Get([FromRoute(Name = "admin_working_years_record_id")] long adminWorkingYearsRecordId)
        {
            return Ok(this.adminWorkingYearsRecordService.Get(adminWorkingYearsRecordId));
        }

        #endregion

        #region 获取员工工作经历信息接口（分页）
        /// <summary>
        /// 获取员工工作经历信息列表（分页）
        /// </summary>
        /// <param name="adminFullName">员工姓名</param>
        /// <param name="idCard">身份证</param>
        /// <param name="phone">手机号</param>
        /// <param name="enterpriseName">公司名称</param>
        /// <param name="adminId">员工ID</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>       
        /// <param name="ascending">排序</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">带有分页信息的员工工作经历列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<AdminWorkingYearsRecordInfoDto>), 200)]
        public IActionResult List(
            [FromQuery(Name = "user_name")]string adminFullName,
            [FromQuery(Name = "id_card")]string idCard,
            [FromQuery(Name = "phone")]string phone,
            [FromQuery(Name = "enterprise_name")]string enterpriseName,
            [FromQuery(Name = "admin_id")] long? adminId, 
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "ascending")]bool? ascending,
            int pageIndex = 1,
            int pageSize = 10)
        {
            //var list = this.adminWorkingYearsRecordService.List(adminFullName, idCard, phone, enterpriseName,adminId, start, end, ascending, pageIndex, pageSize);
            //return this.Ok(list);

            if (this.Admin.IsMain)
            {
                var list = this.adminWorkingYearsRecordService.List(this.Admin.CompanyId, null, adminFullName, idCard, phone, enterpriseName, adminId, start, end, ascending, pageIndex, pageSize);
                return this.Ok(list);
            }
            else
            {
                if (this.Admin.Department.PrincipalAdminId == this.Admin.AdminId)
                {
                    var list = this.adminWorkingYearsRecordService.List(this.Admin.CompanyId, this.Admin.Department.DepartmentId, adminFullName, idCard, phone, enterpriseName, adminId, start, end, ascending, pageIndex, pageSize);
                    return this.Ok(list);
                }
                else
                {
                    var list = this.adminWorkingYearsRecordService.List(this.Admin.CompanyId, this.Admin.Department.DepartmentId, adminFullName, idCard, phone, enterpriseName, adminId, start, end, ascending, pageIndex, pageSize);
                    return this.Ok(list);
                }
            }

        }
        #endregion
    }
}
